package de.awg.accountanalyzer.entities;

import java.util.HashSet;
import java.util.Set;

public class Category extends ChartEntity implements Comparable<Category> {

	private String categoryName;
	private Set<Account> accountList;

	public Category(String pCategoryName) {
		categoryName = pCategoryName;
		accountList = new HashSet<Account>();
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public Set<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(Set<Account> accountList) {
		this.accountList = accountList;
	}

	public void addAccount(Account ac) {
		accountList.add(ac);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((categoryName == null) ? 0 : categoryName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Category other = (Category) obj;
		if (categoryName == null) {
			if (other.categoryName != null)
				return false;
		} else if (!categoryName.equals(other.categoryName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("Kategorie: ");
		buffer.append(this.getCategoryName());
		buffer.append("\n");

		for (Account account : accountList) {
			buffer.append(account);
			buffer.append("\n");
		}

		return buffer.toString();
	}

	@Override
	public int compareTo(Category o) {
		return this.getCategoryName().compareTo(o.getCategoryName());
	}

	@Override
	public double getValue() {
		double result = 0;
		for (Account account : accountList) {
			result += account.getValue();
		}
		return result;
	}

	@Override
	public String getName() {
		return this.categoryName;
	}
}
